package com.example.shuimu;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class KeyBoardDialog extends Dialog implements View.OnClickListener  {
    private EditText et_bag_id;
    private TextView tv_guide_count;
    private KeyBoardDialog keyBoardDialog;
    private long countDownInterval = 1000;
    // 倒计时时间设置
    private static final int secondCountdownToClosing = 60;
   // private CountDownTimerSupport mTimer;
    public KeyBoardDialog(@NonNull Context context) {
        super(context);
    }

    public KeyBoardDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected KeyBoardDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mydia);
        //按空白处不能取消动画
        //setCanceledOnTouchOutside(false);
        //设置dialog样式
        Window window =getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
       // window.setBackgroundDrawableResource(R.drawable.drop_background);
        intView();
        //setTimer();
    }
    /**
     private void setTimer() {
     //总时长 间隔时间
     long millisInFuture = secondCountdownToClosing * 1000L;
     mTimer = new CountDownTimerSupport(millisInFuture, countDownInterval);
     // SimpleOnCountDownTimerListener
     mTimer.setOnCountDownTimerListener(new OnCountDownTimerListener() {
    @Override
    public void onTick(long millisUntilFinished) {
    // 倒计时间隔
    tv_guide_count.setText(millisUntilFinished / 1000 + "s");
    }

    @Override
    public void onFinish() {
    // 倒计时结束
    dismiss();
    }

    @Override
    public void onCancel() {
    // 倒计时手动停止
    }
    });
     mTimer.start();
     }
     */
    private void intView() {
        tv_guide_count = findViewById(R.id.tv_guide_count);
        et_bag_id = findViewById(R.id.et_bag_id);
        et_bag_id.setInputType(InputType.TYPE_NULL);
        findViewById(R.id.tv_0).setOnClickListener(this);
        findViewById(R.id.tv_0).setOnClickListener(this);
        findViewById(R.id.tv_1).setOnClickListener(this);
        findViewById(R.id.tv_2).setOnClickListener(this);
        findViewById(R.id.tv_3).setOnClickListener(this);
        findViewById(R.id.tv_4).setOnClickListener(this);
        findViewById(R.id.tv_5).setOnClickListener(this);
        findViewById(R.id.tv_6).setOnClickListener(this);
        findViewById(R.id.tv_7).setOnClickListener(this);
        findViewById(R.id.tv_8).setOnClickListener(this);
        findViewById(R.id.tv_9).setOnClickListener(this);
        findViewById(R.id.tv_del).setOnClickListener(this);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.iv_guide_count).setOnClickListener(this);
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
    }
    /**
     * 设置确定取消按钮的回调
     */
    public KeyDialogOnClickListener keyDialogOnClickListener;
    public KeyBoardDialog setOnClickKeyDialogListener(KeyDialogOnClickListener keyDialogOnClickListener) {
        this.keyDialogOnClickListener = keyDialogOnClickListener;
        return this;
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.tv_0){
            et_bag_id.getText().append("0");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_1){
            et_bag_id.getText().append("1");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_2){
            et_bag_id.getText().append("2");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_3){
            et_bag_id.getText().append("3");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_4){
            et_bag_id.getText().append("4");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_5){
            et_bag_id.getText().append("5");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_6){
            et_bag_id.getText().append("6");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_7){
            et_bag_id.getText().append("7");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_8){
            et_bag_id.getText().append("8");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_9){
            et_bag_id.getText().append("9");
            //将光标设置到尾部
            et_bag_id.setSelection(et_bag_id.getText().length());
        }else if (view.getId()==R.id.tv_del){
            if (et_bag_id.getText().length()!=0){
                et_bag_id.getText().delete(et_bag_id.getText().length()-1,et_bag_id.getText().length());
            }
        }else if (view.getId()==R.id.tv_confirm){
            keyDialogOnClickListener.onPositiveClick(et_bag_id.getText().toString());
            et_bag_id.getText().delete(0,et_bag_id.getText().length());
        } else if (view.getId()==R.id.iv_guide_count) {
            keyDialogOnClickListener.onQuitClick();
            dismiss();
        }
    }
    public interface KeyDialogOnClickListener{
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String password);
        /**
         * 点击取消按钮事件
         */
        public void onQuitClick();
    }

}
